import {defineStore} from 'pinia';
import {Http, Cache, Helper, Msg, Constant, Log, GlobalConfig} from 'core';
const config = GlobalConfig();
/**
 * 押金处理
 * @type {{id: string, state(): {}, getters: {}, actions: {}}}
 */
const store = {
    id: "deposit",
    state() {
        return {
            depositTravelRecords: [],
            depositInfo: {},
        }
    },

    getters: {},

    actions: {
        /**
         * 读取押金信息
         * @param orderNo
         * @returns {Promise<*|{}>}
         */
        async getInfo(orderNo) {
            Msg.loading("加载中...")
            this.depositInfo = {};
            let {code, data} = await Http.get(`/orders/getOrderDepositInfo/${orderNo}`);
            Msg.loading(false);
            if(code == 200) {
                this.depositInfo = data;
            }

            return this.depositInfo;
        },


        /**
         * 订单押金扣款
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         */
        async payAuth(orderNo, optDeposit, remark, fileArr) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/depositPayAuth", {orderNo, optDeposit, remark, fileArr});
            Msg.loading(false);
            if(code == 200) {
                // Msg.success("扣款成功");
                Msg.confirm("已发起扣款，请随时检查账户是否到账")
                return true;
            }

            return false;
        },

        /**
         * 押金免密代扣
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         * @returns {Promise<boolean>}
         */
        async withhold(orderNo, optDeposit, remark, fileArr) {
            //
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/withholdOrderDeposit", {orderNo, optDeposit, remark, fileArr});
            Msg.loading(false);
            if(code == 200) {
                Msg.confirm("已发起扣款，请随时检查账户是否到账")
                return true;
            }

            return false;
        },

        /**
         * 获取押金进度
         * @param orderNo
         * @returns {Promise<void>}
         */
        async getOrderDepositTravel(orderNo) {
            Msg.loading("加载中...");
            this.depositTravelRecords = [];
            let {code, data} = await Http.get(`/orderPay/getOrderDepositTravel/${orderNo}`);
            if(code == 200) {
                if(! Helper.emptyArray(data)) {
                    data.forEach(e => {
                        this.depositTravelRecords.push({title: e.optTime, desc: e.remark})
                    })
                }
            }
            Msg.loading(false);
        },

        /**
         * 订单押金退款
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         */
        async refunded(id, optDeposit, remark, fileArr) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/refundedOrderDeposit", {recordId: id, optDeposit, remark, fileArr});
            Msg.loading(false);
            if(code == 200) {
                Msg.success("退押成功");
                return true;
            }

            return false;
        },

        /**
         * 订单押金解约
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         */
        async unfreeze(orderNo) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/unfreezeOrder/" + orderNo);
            Msg.loading(false);
            if(code == 200) {
                Msg.success("解约成功");
                return true;
            }

            return false;
        },

        /**
         * 押金取消扣款
         * @param orderNo
         */
        async depositPayCancel(orderNo) {
            Msg.loading("加载中...")
            let {code, data} = await Http.get("/orderPay/depositPayCancel/" + orderNo);
            Msg.loading(false);
            if(code == 200) {
                Msg.success("取消成功");
                return true;
            }

            return false;
        },

        formatType(type) {
            let obj = config.productConfig.depositTypeDict.find(x => x.value == type);
            return obj ? obj.label : "";
        }
    }
}

export default defineStore(store)
